
sub check_ball_2screen( byref ball as big_hairy_ballxorz_struct, byval loop_time as single )

    if fgrav>0 then

        if ball.p.y>479-ball.r then

            ball.p.y=479-ball.r
            ball.v.y*=-.45
            ball.v.x*=.35

            if abs(ball.v.y)<18.0*loop_time then ball.v.y = 0

        end if

        if ball.p.y-ball.r<0 then

            ball.p.y=ball.r
            ball.v.y*=-.45

                if ball.v.y <= 0 then
                ball.v.y = .45
                end if

        end if

    elseif fgrav<0 then

        if ball.p.y>479-ball.r then

            ball.p.y=479-ball.r
            ball.v.y*=-.45

            if ball.v.y >= 0 then
                ball.v.y = -.45
            end if

        end if

        if ball.p.y-ball.r<0 then

            ball.p.y=ball.r
            ball.v.y*=-.45
            ball.v.x*=.35

            if abs(ball.v.y)<18.0*loop_time then ball.v.y = 0

        end if

    end if


    if ball.p.x>639-ball.r then

        ball.p.x=639-ball.r
        ball.v.x*=-.45

        if abs(ball.v.x)<18.0*loop_time then ball.v.x = 0

    end if

    if ball.p.x-ball.r<0 then

        ball.p.x=ball.r
        ball.v.x*=-.45

        if abs(ball.v.x)<18.0*loop_time then ball.v.x = 0

    end if

end sub

''******playNextSong******
function playNextSong() as integer
    static song_channel as integer = 1
    static old_song as integer = -1
    dim playlist as ext.xml.tree
    playlist.load("snd/music/playlist.xml")
    var max_songs = playlist.root->child(0)->children("track") -1

    var randsong = RndRange(0,max_songs)
    while randsong = old_song
        randsong = RndRange(0,max_songs)
    wend
    old_song = randsong
    var thissong = playlist.root->child(0)->child("track",randsong)
    var sngfile = thissong->attribute("src")

    FSOUND_Stream_Close( song )
    song = FSOUND_Stream_Open( sngfile, FSOUND_LOOP_OFF, 0, 0 )
    Messages.push( new notification("Now Playing",thissong->child("title")->getText() & " by", _
    thissong->child("artist")->getText(), 3, ext.gfx.loadimage("../img/notes_music.png") ) )
    con.WriteLine("Now Playing: " & thissong->child("title")->getText() & " by " & thissong->child("artist")->getText())
    song_channel = FSOUND_Stream_Play( song_channel, song )
    var temp_3d = ext.math.vec3f(320,240,0)
    FSOUND_3D_SetAttributes( song_channel, @temp_3d.x, 0 )
    return song_channel

end function

''******in_aabb******
function in_aabb( byref v1 as vec2f, byref v2 as vec2f, byref box as integer ) as integer

    if abs(v1.x-v2.x)<=box then

        if abs(v1.y-v2.y)<=box then return 1

    end if

    return 0

end function

function mixalpha( byval colour as uinteger, byval mixer as integer ) as uinteger

    var x = cuint(((colour AND &hff000000) shr 24) / mixer) shl 24

    return (x or (colour AND &h00ffffff))

end function

''******scaletoubyte******
function scaletoubyte(byval v as integer) as integer

    var ret = v
    if v > 255 then ret = 255
    if v < 0 then ret = 0

    return ret

end function


''******LoadSettings******
sub LoadSettings( byref wf as integer, byref mvol as integer, byref svol as integer, byref sfps as integer )

    dim as ext.xml.tree xtree
    chdir exepath()
    xtree.load(CONFIG_DIR & "settings.xml")
    if xtree.root->child(0) = null then 'create settings file
        xtree.root->appendChild("settings")->appendChild("wireframe-mode")
        xtree.root->child(0)->child(0)->attribute("value") = str(1)

        xtree.root->child(0)->appendChild("music-volume")
        xtree.root->child(0)->child(1)->attribute("value") = "127"

        xtree.root->child(0)->appendChild("sfx-volume")
        xtree.root->child(0)->child(2)->attribute("value") = "127"

        xtree.root->child(0)->appendChild("show-fps")
        xtree.root->child(0)->child(3)->attribute("value") = "0"

        xtree.unload(CONFIG_DIR & "settings.xml")
    end if

    wf = val(xtree.root->child(0)->child("wireframe-mode")->attribute("value"))

    mvol = scaletoubyte( val(xtree.root->child(0)->child("music-volume")->attribute("value")) )

    svol = scaletoubyte( val(xtree.root->child(0)->child("sfx-volume")->attribute("value")) )

    sfps = val(xtree.root->child(0)->child("show-fps")->attribute("value"))

end sub


''******Load_SFX******
sub Load_Sfx( byref filename as string, _SFX() as FSound_Sample ptr )

    static as integer sfx_count
    sfx_count+=1
    dim as integer i

    redim preserve _SFX( 1 to sfx_count )

    _Sfx(sfx_count) = FSOUND_Sample_Load(FSOUND_FREE, filename, FSOUND_HW3D or FSOUND_LOOP_OFF, 0, 0)
    'FSOUND_3D_SetRolloffFactor( 1 )

    if _sfx(sfx_count) = 0 then con.WriteLine("Error loading: " & filename)

end sub
